import base64
import os

# Constant Variable for system
ROOT_PATH = os.path.dirname(__file__)
VIEW_PATH = os.path.join(ROOT_PATH, 'view')

class ExternalQuestionForm:
    AcceptedHeader = '<html><body><form action="http://www.mturk.com/mturk/externalSubmit" method="post">'
    AcceptedFooter = '<input type="submit" value="Submit" /></form></body></html>'
    PreviewHeader = '<html><body>'
    PreviewFooter = '</body></html>'
    
class ParameterKey:
    """
    keys for query string
    """
    OPERATION = "op"
    REDIRECT_URL = "re"
    COMPANY_NAME = "org"
    NEW_PASSWORD = "newpassword"
    OLD_PASSWORD = "oldpassword"
    HIT_Properties_Template_ID = "hpid"
    HIT_Template_ID = "htid"
    CLIENT_ID = "cid"
    RENDERER_JSON = "rendererjson"
    HIT_PREVIEW = "assignmentId"
    QUESTION_ID = "questionid"


class FormData:
    """
    Keys in post back request
    """
    USER_NAME = "UserName"
    PASSWORD = "Password"
    ORG_NAME = "OrgName"
    REDIRECT_URL = "RedirectURL"
    EMAIL = "EMail"
    #
    COMPANY_NAME = "CompanyName"
    GROUP_NAME = "GroupName"
    CLIENT_NAME = "ClientName"
    CAMPAIGN_NAME = "CampaignName"
    #
    COMPANY_KEY = "ddl_companykey"   # ddl stands for dropdown list
    GROUP_KEY = "ddl_groupkey"
    CLIENT_KEY = "ddl_clientkey"
    CAMPAIGN_KEY = "ddl_campaignkey"
    #
    HIT_TEMPLATE_NAME = 'hit_template[name]'
    HIT_TEMPLATE_TITLE = 'hit_template[title]'
    HIT_TEMPLATE_DESCRIPTION = 'hit_template[description]'
    HIT_TEMPLATE_KEYWORDS = 'hit_template[keywords]'
    HIT_TEMPLATE_ASSIGNMENT_DURATION = 'hit_template[assignment_duration_fields][value]'
    HIT_TEMPLATE_ASSIGNMENT_DURATION_UNIT = 'hit_template[assignment_duration_fields][units]'
    HIT_TEMPLATE_LIFETIME = 'hit_template[lifetime_fields][value]'
    HIT_TEMPLATE_LIFETIME_UNIT = 'hit_template[lifetime_fields][units]'
    HIT_TEMPLATE_REWARD = 'hit_template[reward_dollars]'
    HIT_TEMPLATE_MXA_ASSIGNMENTS = 'hit_template[max_assignments]'
    HIT_TEMPLATE_AUTO_APPROVAL_DELAY = 'hit_template[auto_approval_delay_fields][value]'
    HIT_TEMPLATE_AUTO_APPROVAL_DELAY_UNIT = 'hit_template[auto_approval_delay_fields][units]'
    TIME_UNIT_DAY = 'Days'
    TIME_UNIT_HOUR = 'Hours'
    TIME_UNIT_MINUTE = 'Minutes'
    HIT_TEMPLATE_QUESTION_NAME="question_template[name]"
    HIT_TEMPLATE_QUESTION_FRAMEHEIGHT="question_template[height]"
    HIT_TEMPLATE_QUESTION_METAHTML="question_template[html]"
    # upload data
    UPLOAD_FILE = "uploadfile"

class ViewName:
    LOGIN = "login"
    DEMO = "demo"
    HOME = "index"
    PROFILE = "profile"
    D_CREATECOMPANY = "demo_createcompany"
    D_CREATECOMPANY_RESPONSE = "demo_createcompany_response"
    D_CREATEUSER = "demo_createuser"
    D_CREATECLIENT = "demo_createclient"
    D_CREATECAMPAIGN = "demo_createcampaign"
    D_CREATEGROUP = "demo_creategroup"
    CREATE_HIT = "createhit"
    CREATE_HIT_TEMPLATE = "createHITTemplate"
    FORBIDDENPAGE = "403"
    CREATE_HIT = "createhit"


class TemplateValueName:
    ORG_NAME = 'OrgName'
    REDIRECT_URL = 'RedirectURL'
    ERROR_MESSAGE = 'ErrorMsg'
    SUCCESS_MESSAGE = 'SuccessMsg'
    HAS_ORG_NAME = 'HasOrgName'
    CAMPAIGNS = "Campaigns"
    CLIENTS = "Clients"


class UserRole:
    SystemAdmin = "SysAdmin"
    CompanyAdmin = "CompanyAdmin"
    GroupAdmin = "GroupAdmin"
    User = "User"

class SessionKey:
    CURRENT_USER = "currentuser"
    CURRENT_HIT_DATA = "currenthitdata"
    CURRENT_HIT_CSV_DATA = "currenthitcsvdata"
    CURRENT_HIT_CAMPAIGN = "currenthitcampaign"
    CURRENT_HIT_FormTemplateData = "currenthittemplate"

# methods
def IsEmptyString(str):
    return (str is None) or (str.strip() == "")


def Encrypt(str):
    if not IsEmptyString(str):
        return base64.b16encode(str)
    else:
        return str


def Decrypt(str):
    if not IsEmptyString(str):
        return base64.b64decode(str)
    else:
        return str


def TransferToSeconds(value, unit):
    minute = 3600
    hour = minute * 60
    day = hour * 24
    if unit == FormData.TIME_UNIT_DAY:
        return value * day
    elif unit == FormData.TIME_UNIT_HOUR:
        return value * hour
    elif unit == FormData.TIME_UNIT_MINUTE:
        return value * minute



